package com.ybu.oodd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ybu.oodd.mapper.JobMapper;
import com.ybu.oodd.pojo.Job;
import com.ybu.oodd.pojo.PageResult;
import com.ybu.oodd.pojo.QueryParams;
import com.ybu.oodd.pojo.Status;
import com.ybu.oodd.service.JobService;
import com.ybu.oodd.utils.login.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper jobMapper;

    /*
     * 条件分页查询岗位
     * */
    @Override
    public PageResult<Job> getJobs(QueryParams sp) {
        //补充查询条件userId
        sp.setUserId(ContextHolder.get("userId"));
        // 分页助手
        PageHelper.startPage(sp.getPage(),sp.getPageSize());

        List<Job> rows = new ArrayList<>();

        if(sp.getStatusId() == null ){
            rows = jobMapper.getJobsNull(sp);
        } else if (sp.getStatusId() == 16) {
            rows = jobMapper.getJobsES(sp);
        } else{
            rows = jobMapper.getJobs(sp);
        }

        //结果转换
        Page<Job> p = (Page<Job>) rows;
        return new PageResult<Job>(p.getTotal(), p.getResult());
    }

    /*
     * 根据userId和jobId更新statusId
     * */
    @Override
    public boolean updateStatus(Status status) {
        //获取userId
        status.setUserId(ContextHolder.get("userId"));
        //先检查是否有投递记录
        Integer oldStatusId = jobMapper.getStatusId(status);
        //有 则修改
        if(oldStatusId != null){
            jobMapper.updateStatus(status);
            return true;
        }//无 则新增
        else{
            jobMapper.insertStatus(status);
            return true;
        }
    }

    /*
     * 查询公司类型
     * */
    @Override
    public List<Map<Integer, String>> getCompanyType() {
        return jobMapper.getKeywordByTypeIsLiked("company_type_id",null,true);
    }

    /*
     * 查询 校招招聘类型
     * */
    @Override
    public List<Map<Integer, String>> getSchoolJobType() {
        return jobMapper.getKeywordByTypeIsLiked("job_type_id","实习",false);
    }

    /*
     * 查询 实习招聘类型
     * */
    @Override
    public List<Map<Integer, String>> getInternJobType() {
        return jobMapper.getKeywordByTypeIsLiked("job_type_id","实习",true);
    }

    /*
     * 查询 所有招聘类型
     * */
    @Override
    public List<Map<Integer, String>> getJobType() {
        return jobMapper.getKeywordByTypeIsLiked("job_type_id","",true);
    }

    /*
     * 查询招聘对象
     * */
    @Override
    public List<Map<Integer, String>> getCandidateType() {
        return jobMapper.getKeywordByTypeIsLiked("candidate_id",null,true);
    }

    /*
     * 查询学历
     * */
    @Override
    public List<Map<Integer, String>> getEducationalBackgroundType() {
        return jobMapper.getKeywordByTypeIsLiked("educational_background_id",null,true);
    }

    /*
     * 查询投递状态类型
     * */
    @Override
    public List<Map<Integer, String>> getStatusType() {
        return jobMapper.getKeywordByTypeIsLiked("status_id",null,true);
    }
}
